//
//  ALSecondTableViewCell.m
//  Ummy.G
//
//  Created by 安林 on 16/9/28.
//  Copyright © 2016年 安林. All rights reserved.
//

#import "ALSecondTableViewCell.h"
#import "ALTwoCell.h"
#import "ALTwoModel.h"


@implementation ALSecondTableViewCell
static NSString *ID = @"cell";





- (NSMutableArray *)arrayM{

    if (!_arrayM) {
        _arrayM = [NSMutableArray array];
    }
    return _arrayM;
}


- (void)awakeFromNib {
    
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    
        
    //注册cell（把那个小的xib添加上去）
    [self.collectionView registerNib:[UINib nibWithNibName:@"ALTwoCell" bundle:nil] forCellWithReuseIdentifier:ID];
    [self addLoadData];
    
    _Imgarray = @[@"about_criticism",@"about_praise",@"bg_negative_redPacket",@"icon_review_easter_egg",@"love_10",@"ad_03"];
    
    //禁止第二个界面上下滑动
    self.collectionView.scrollEnabled = NO;
    
}

#pragma mark 用代理必须实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.arrayM.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    ALTwoModel *model = self.arrayM[indexPath.row];
    ALTwoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.model = model;
    cell.ImageView.image=[UIImage imageNamed:self.Imgarray[indexPath.row]];
    return cell;
}


#pragma mark 加载数据(首页中第二个模块)
-(void)addLoadData{
   
    //定义一个管理者
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSString *url = @"http://api.meituan.com/group/v1/deal/topic/discount/city/76?version_name=7.3.1&limit=7&latlng=38.042167%2C114.477345&__vhost=api.mobile.meituan.com&utm_source=huawei&utm_medium=android&utm_term=431&utm_content=a0000055de24e8&utm_campaign=AgroupBgroupC0D200E0&ci=76&msid=a0000055de24e81474794199082&uuid=FDC1C5960287691C1AA0FDB6CE8736766612182AEF7A556CA2D3DB97774FE2E8&userid=180479389&__reqTraceID=879b90ed-d179-41a7-8266-5871623d2f82&__skck=6a375bce8c66a0dc293860dfa83833ef&__skts=1474796536921&__skua=58810434ff9b9a0bc6d4edd46aea3499&__skno=a936780a-827a-4bd6-a01b-0497c13b11ed&__skcy=ShtdGQ%2BjlGw42b1sWV3sS%2FlwqjY%3D";
    
    //请求数据
    [manger GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //生成桌面的plist文件
        //[responseObject writeToFile:@"/Users/anlin/Desktop/two.plist" atomically:YES];
        //请求数组（请求网络上的数据）
        NSArray *array = responseObject[@"data"];
        for (NSDictionary *dict in array) {
            ALTwoModel *model = [ALTwoModel altwomodelWithDict:dict];
            [self.arrayM addObject:model];
        }
       // NSLog(@"12333%@",self.arrayM);
        //请求完数据之后刷新一下数据
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
       // NSLog(@"%@",error);
    }];
}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}


#pragma mark 让xib显示出来
+(instancetype)loadALSecondTableViewCell{
    return [[[NSBundle mainBundle]loadNibNamed:@"ALSecondTableViewCell" owner:nil options:nil]firstObject];
}


@end
